﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Earthwatchers.Data;
using Earthwatchers.Models;

namespace Earthwatchers.Landgenerator
{

    //var currentLon = topLeft.X;
    //var currentLat = topLeft.Y;

    class Program
    {
        static void Main(string[] args)
        {

            //var topLeft = new Point(107.4, 8.45);
            //var bottomRight = new Point(119.55, -4.28);
            // 7 1.4 km2 
            // 6 
            //var testLand=new Land() {Longitude=0, Latitude=0, GeohexKey= GeoHex.Encode(0, 0, 6)};
            //var listLand=new List<Land> ();
            //listLand.Add(testLand);
            //longitude: -64 -61
            //latitude -23 -27


            //longitude: -64 -59
            //latitude: -24 -27


            var topLeft = new Point(-64, -24);
            var bottomRight = new Point(-59, -27);
            var newLand = GetNewLand(topLeft, bottomRight, 6);

            // write land to database...
            var landRepository = new LandRepository("Data Source=dfrvf2t76i.database.windows.net;Initial Catalog=Earthwatchers;Persist Security Info=True;User ID=Editor;Asynchronous Processing=True;Password=8p3k00l!!!!");
            landRepository.CreateLand(newLand);
            Console.WriteLine("Klaar");
        }


        public static List<Land> GetNewLand(Point topLeft, Point bottomRight, int level)
        {
            var newLand = new List<Land>();
            const double increase = 0.001;

            for (var i = topLeft.X; i <= bottomRight.X; i += increase)
            {
                for (var j = topLeft.Y; j >= bottomRight.Y; j -= increase)
                {
                    var hexKey = GeoHex.Encode(i, j, level);
                    var land = new Land();
                    land.Longitude = i;
                    land.Latitude = j;
                    land.GeohexKey = hexKey;

                    if (!newLand.Any(l => l.GeohexKey == hexKey))
                    {
                        newLand.Add(land);
                    }
                }
                Debug.WriteLine("longitude: " + i.ToString());
            }
            return newLand;
        }
    }


    public class Point
    {
        public Point(double x, double y)
        {
            this.X = x;
            this.Y = y;
        }

        public double X { get; set; }
        public double Y { get; set; }

    }

}
